package com.gin.kotlin.gf2gamecommunity.routes

import androidx.compose.animation.core.tween
import androidx.compose.animation.slideInHorizontally
import androidx.compose.animation.slideOutHorizontally
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import androidx.navigation.toRoute
import androidx.paging.compose.collectAsLazyPagingItems
import com.gin.kotlin.gf2gamecommunity.base.composable.Loading
import com.gin.kotlin.gf2gamecommunity.viewmodel.ScoreViewModel
import kotlinx.serialization.Serializable


fun NavGraphBuilder.scoreLog(onPopBackStack: () -> Unit) = composable<ScoreLogRoute>(
    // 从屏幕右侧进入，持续500毫秒
    enterTransition = { slideInHorizontally(tween(500)) { it } },
    // 从屏幕右侧退出，持续500毫秒
    exitTransition = { slideOutHorizontally(tween(500)) { it } },
) {
    ScoreLogComposable(route = it.toRoute(), onPopBackStack = onPopBackStack)
}

@Serializable
data class ScoreLogRoute(val id: Int = 1)

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun ScoreLogComposable(route: ScoreLogRoute, viewModel: ScoreViewModel = viewModel(), onPopBackStack: () -> Unit) {
    val lazyPagingItems = viewModel.log.fetch(null).collectAsLazyPagingItems()

    Scaffold(
        topBar = {
            TopAppBar(navigationIcon = {
                IconButton(onClick = { onPopBackStack() }) {
                    Icon(imageVector = Icons.AutoMirrored.Filled.ArrowBack, contentDescription = "后退")
                }
            }, actions = {
            }, title = {
                Text("积分记录")
            })
        },

        ) { paddingValues ->
        val modifier = Modifier.padding(paddingValues)

        if (lazyPagingItems.itemCount == 0) {
            Box(modifier = modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
                Loading()
            }
        } else {
            Box(modifier.padding(8.dp)) {
                LazyColumn(verticalArrangement = Arrangement.spacedBy(2.dp), contentPadding = PaddingValues(8.dp)) {
                    items(lazyPagingItems.itemCount) { index ->
                        lazyPagingItems[index]?.apply {
                            Row(horizontalArrangement = Arrangement.SpaceBetween, modifier = Modifier.fillMaxWidth()) {
                                Text("$reason: $score")
                                Text(logTime)
                            }
                        }
                    }
                }
            }
        }


    }
}